package net.esj.basic.core.engine.tools.crypt;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import org.springframework.util.Assert;

import net.esj.basic.core.engine.support.UnsupportedRunTimeException;
import net.esj.basic.utils.StringUtils;

public class HMACSHA1CryptImpl implements UnilateralCrypt {

	private static final String HMAC_SHA1 = "HmacSHA1";   
	
	private String DEFAULT_KEY = "Hasdf*wqyrt*&(drT8EQ3r57rQrq@#yr9e";
	
	@Override
	public boolean checksum(byte[] bytes, byte[] encryptPass, byte[] results) {
		Assert.notNull(results);
		byte[] encrypts = encrypt(bytes, encryptPass);
		
		if(encrypts.length != results.length){
			return false;
		}
		for(int i=0;i<encrypts.length;i++){
			if(encrypts[i] != results[i]){
				return false;
			}
		}
		return true;
	}

	@Override
	public boolean checksum(byte[] bytes, byte[] results) {
		Assert.notNull(results);
		byte[] encrypts = encrypt(bytes);
		
		if(encrypts.length != results.length){
			return false;
		}
		for(int i=0;i<encrypts.length;i++){
			if(encrypts[i] != results[i]){
				return false;
			}
		}
		return true;
	}

	@Override
	public boolean checksum(String content, byte[] encryptPass, String results) {
		Assert.notNull(results);
		String encrypts = encrypt(content,encryptPass);
		
		return results.equals(encrypts);
	}

	@Override
	public boolean checksum(String content, String results) {
		Assert.notNull(results);
		String encrypts = encrypt(content,DEFAULT_KEY.getBytes());
		
		return results.equals(encrypts);
	}

	@Override
	public byte[] decrypt(byte[] bytes) {
		throw new UnsupportedRunTimeException("单向加密不支持解密程式!!!");
	}

	@Override
	public byte[] decrypt(byte[] bytes, byte[] decryptPass) {
		throw new UnsupportedRunTimeException("单向加密不支持解密程式!!!");
	}

	@Override
	public String decrypt(String content) {
		throw new UnsupportedRunTimeException("单向加密不支持解密程式!!!");
	}

	@Override
	public String decrypt(String content, byte[] decryptPass) {
		throw new UnsupportedRunTimeException("单向加密不支持解密程式!!!");
	}

	@Override
	public byte[] encrypt(byte[] bytes) {
		return encrypt(bytes, DEFAULT_KEY.getBytes());
	}

	@Override
	public byte[] encrypt(byte[] bytes, byte[] encryptPass) {
		SecretKeySpec signingKey = new SecretKeySpec(encryptPass, HMAC_SHA1);   
        Mac mac;
		try {
			mac = Mac.getInstance(HMAC_SHA1);
			mac.init(signingKey);   
			byte[] rawHmac = mac.doFinal(bytes);   
			return rawHmac;
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		}  
		return null;
	}

	@Override
	public String encrypt(String content) {
		byte[] bytes = content.getBytes();
		byte[] crypts = encrypt(bytes);
		return StringUtils.parseByte2HexStr(crypts);
	}

	@Override
	public String encrypt(String content, byte[] encryptPass) {
		byte[] bytes = content.getBytes();
		byte[] crypts = encrypt(bytes,encryptPass);
		return StringUtils.parseByte2HexStr(crypts);
	}

	@Override
	public void setDefaultSecretKey(String key) {
		DEFAULT_KEY = key;
	}

}
